package ru.ipccenter.orlov;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 *
 * @author GigaIce
 */
@ManagedBean
@SessionScoped
public class Authenticator
{

    private String login;
    private String userPassword;
    final private String errorMessage = "Your Login or password is incorrect";
    boolean errorWhileAuthentication = false;
    final String sault = "VasyaOrlov";
    
    public Authenticator()
    {}
    
    
    
    /*public String authenticate()
    {
        Users user;
        try
        {
           user = getUserByLogin( login);
        }
        catch( UserNotFoundException e)
        {
            errorWhileAuthentication = true;
        }
        
        boolean isCorrectPassword = checkPassword( user.getPasswordHashCode());
        if( isCorrectPassword)
        {
            return AviaAgency.welcomePage;
        }
        else
        {
            errorWhileAuthentication = true;
            return null;
        }
        
        /*private Users getUserByLogin( String login)
        {
            return null
        }
        */
        private boolean checkPassword( byte[] passwordHashCode)
        {
            MessageDigest md = null;
            try
            {
                md = MessageDigest.getInstance("MD5");
            }
            catch ( NoSuchAlgorithmException e)
            {
                AviaAgency aviaAgency = new AviaAgency();
                aviaAgency.setErrorMessage("Can't find MD5 Algorithm");
            }
            md.update( ( sault + userPassword).getBytes());
            byte[] md5 = md.digest();      
            return Arrays.equals(md5, passwordHashCode);       
        }
        
    }
//}

class UserNotFoundException extends Exception
{}
